﻿using System.Drawing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace LEDHelper
 {
        static public class FontHelper
        {
            static public string GetBmp(string str,string fontname, int fontsize, int point,ref int FID)
            {
                #region Font
                System.Drawing.Text.PrivateFontCollection colFont = new System.Drawing.Text.PrivateFontCollection();
                string fontfile = "黑体";
                switch (fontname)
                { 
                    case"黑体":
                        fontfile=Path.Combine(PathHelper.ApplicationPath, @"Font\simhei.ttf");
                        break;
                    case"仿宋":
                        fontfile = Path.Combine(PathHelper.ApplicationPath, @"Font\simfang.ttf");
                        break;
                    case "宋体":
                        fontfile = Path.Combine(PathHelper.ApplicationPath, @"Font\simsun.ttc");
                        break;
                    case "隶书":
                        fontfile = Path.Combine(PathHelper.ApplicationPath, @"Font\SIMLI.TTF");
                        break;
                    default:
                        fontfile = Path.Combine(PathHelper.ApplicationPath, @"Font\simsun.ttc");
                        break;
                }
                colFont.AddFontFile(fontfile);
                Font f = new Font(colFont.Families[0], fontsize, FontStyle.Regular, GraphicsUnit.Point);
                #endregion
                Guid gf = Guid.NewGuid();
                if (!Directory.Exists(Path.Combine(PathHelper.ApplicationPath, @"TextB")))
                {
                    Directory.CreateDirectory(Path.Combine(PathHelper.ApplicationPath, @"TextB"));
                }
                if (!Directory.Exists(Path.Combine(PathHelper.ApplicationPath, @"TextB\"+gf.ToString())))
                {
                    Directory.CreateDirectory(Path.Combine(PathHelper.ApplicationPath, @"TextB\"+gf.ToString()));
                }

                string filePath = Path.Combine(PathHelper.ApplicationPath, @"TextB\" + gf.ToString());

                Bitmap bm = new Bitmap(point, point, System.Drawing.Imaging.PixelFormat.Format24bppRgb);                            //最后生成的位图文件写入这个中
                Graphics g = Graphics.FromImage(bm);                        //存从屏幕考出来的图 
                char[] x = str.ToCharArray();
              
                FID = 0;
                foreach (char stc in x)
                {
                    g.Clear(Color.Black);
                    g.DrawString(str.ToString(), f, Brushes.Red, new Point(0, 0));
                    bm.Save(filePath+"\\F"+FID+".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    FID++;
                }                
                bm.Dispose();
                g.Dispose();

                return filePath;
            } 
        }
}